package cc.yuxiang.spider;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.ReUtil;
import jodd.http.HttpRequest;
import jodd.io.FileUtil;
import jodd.io.NetUtil;
import jodd.jerry.Jerry;
import jodd.jerry.JerryFunction;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@AllArgsConstructor
public class Xiumeim {
	private String savePath;
	private static final String url_root = "http://www.xiumeim.com/";
	
	public void toTryDown(Model model) throws UnsupportedEncodingException {
		log.info("模特:{}",model);
		List<ModelDetail> details = this.getDetails(model);
		this.downPic(model, details);
		
		ModelDetail modelDetail = details.get(0);
		
		Integer totalPage = modelDetail.getTotalPage();
		String houzui =".html";
		String tempUrl = model.getUrl().replace(houzui, "");
		for (int i = 2; i < totalPage+1; i++) {
			String urlNew = StrFormatter.format("{}-{}{}", tempUrl,i,houzui);
			log.info("页数:{} 组装新的url详情页:{}",totalPage,urlNew);
			model.setUrl(urlNew);
			details = this.getDetails(model);
			this.downPic(model, details);
		}
		
	}
	
	private void downPic(Model model,List<ModelDetail> details) {
		details.forEach(detail->{
			try {
				String extName = cn.hutool.core.io.FileUtil.extName(detail.getUrl());
				
				File file = new File(savePath+File.separator+model.getTitle());
				if(!FileUtil.isExistingFolder(file)) {
					file = FileUtil.mkdir(file);
				}
				File pic = new File(file.getAbsolutePath()+File.separator+detail.getName()+"."+extName);
				if(!FileUtil.isExistingFile(pic)) {
					NetUtil.downloadFile(detail.getUrl(),pic);
					log.info("{}",pic.getName());	
				}else {
					log.info("存在:{}",pic.getName());
				}
					
			} catch (IOException e) {
				e.printStackTrace();
			}
		});
	}
	
	
	
	private List<ModelDetail> getDetails(Model model) throws UnsupportedEncodingException {
		String body = HttpRequest.get(model.getUrl()).send().bodyText();
		body = new String(body.getBytes("ISO-8859-1"), "UTF-8");
		Jerry $doc = Jerry.jerry(body);
		Integer totalPage = ReUtil.getFirstNumber($doc.$("div.paginator span.count").html());;
		Integer thisPage = Convert.toInt($doc.$("div.paginator span.thispage").html());
		
		List<ModelDetail> details = new ArrayList<>();
		$doc.$("div.gallary_wrap div.gallary_item").each(new JerryFunction() {
			@Override
			public Boolean onNode(Jerry $this, int index) {
				Jerry $img = $this.$("img.photosImg");
				String url = $img.attr("src");
				String alt = $img.attr("alt");
				details.add(new ModelDetail(alt, url,thisPage,totalPage));
				return true;
			}
		});
		return details;
	}

	public List<Model> getRoots() throws UnsupportedEncodingException {
		String body = HttpRequest.get(url_root).send().body();
		body = new String(body.getBytes("ISO-8859-1"), "UTF-8");
		
		Jerry $doc = Jerry.jerry(body);
		List<Model> models = new ArrayList<>();
		$doc.$("div.innercol div.gallary_wrap div.gallary_item_album").each(new JerryFunction() {
			@Override
			public Boolean onNode(Jerry $this, int index) {
				Jerry $a = $this.$("a.photosUrl");
				String url = $a.attr("href");
				Jerry $img = $this.$("img");
				
				String alt = $img.attr("alt");
				String title = alt.substring(alt.indexOf("]")+1, alt.indexOf("[", 1)).trim();
				
				ModelType modelType = doAlt(alt);
				log.info("类型:{} | 标题:{} | 详情页:{}",modelType.getType(),title,url);
				
				models.add(new Model(title, url, modelType));
				return true;
			}

		});
		return models;
	}
	
	private ModelType doAlt(String alt) {
		Jerry $model = Jerry.jerry(alt).$("a.modelUrl");
		String modelUrl = $model.attr("href");
		String modelType = $model.text();
		return new ModelType(modelType, modelUrl);
	}
	
	@Data
	@AllArgsConstructor
	private static class Model {
		private String title;
		private String url;
		private ModelType modelType;
		
	}
	
	@Data
	@AllArgsConstructor
	public static class ModelType{
		private String type;
		private String url;
	}
	
	@Data
	@AllArgsConstructor
	public static class ModelDetail{
		private String name;
		private String url;
		private Integer thisPage;
		private Integer totalPage;
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		String local = "E:\\mm";//本地存放图片
		Xiumeim xiumeim = new Xiumeim(local);
		List<Model> roots = xiumeim.getRoots();
		roots.forEach(model->{
			try {
				xiumeim.toTryDown(model);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		});
	}
}
